Saltar al contenido principal

Windows Architecture and Memory management

Que show​

En Windows, el procesador opera en dos modos distintos para garantizar la estabilidad y seguridad del sistema: User mode y Kernel mode. La principal diferencia radica en los niveles de privilegio y el acceso a los recursos de hardware.

  • User mode: Es un entorno restringido donde se ejecutan las aplicaciones de usuario (como navegadores o editores de texto). Cada proceso tiene su propio espacio de direcciones virtual privado, lo que impide que una aplicación acceda directamente al hardware o a la memoria de otras aplicaciones. Si una aplicación en este modo falla, solo se cierra esa aplicación sin afectar al resto del sistema.

  • Kernel mode: Es un modo privilegiado reservado para los componentes centrales del sistema operativo, el núcleo y los controladores de hardware. Todo el código en este modo comparte un único espacio de direcciones virtual y tiene acceso ilimitado a todos los recursos del sistema. Un error en este modo es catastrófico y suele provocar la caída total del sistema (pantalla azul).

User Mode​

  • System Processes - These are important Windows processes that support the operating system itself. Examples include the Service Control Manager, lsass.exe, winlogon.exe, smss.exe, and csrss.exe. Although these processes are part of the operating system, they still execute in user mode. This is an important distinction because not every operating system component runs in kernel mode.

  • Services - Windows services are background programs that usually run without direct user interaction. Examples include services hosted by svchost.exe, the Print Spooler service running as spoolsv.exe, and WMI components such as WmiPrvSE.exe. Services are commonly used by Windows itself, third-party software, endpoint security products, and enterprise management tools

  • Applications - Applications are normal user-mode programs. Examples include Task Manager, File Explorer, PowerShell, Microsoft Word, Google Chrome, or custom programs compiled during this course. Applications commonly call functions exported by subsystem DLLs such as kernel32.dll, KernelBase.dll, user32.dll, gdi32.dll, advapi32.dll, and others. These DLLs expose many functions from the Windows API. For example:

    • CreateFileW - Used to create or open files, directories, devices, pipes, and other objects.
    • VirtualAlloc - Used to reserve, commit, or change the state of pages in the virtual address space of the calling process.
    • VirtualProtect - Used to change the protection of committed pages.
    • CreateProcessW - Used to create a new process and its primary thread.
    • CreateThread - Used to create a thread in the current process.
    • CreateRemoteThreadEx - Used to create a thread in another process.
    • RegOpenKeyExW - Used to open a registry key.
    • MessageBoxW - Used to display a message box.
  • Windows Subsystem - The Windows subsystem provides the environment that most Windows applications use. This includes the Win32 API environment, which exposes the APIs commonly documented under the Windows API. Modern Windows can also provide other user-mode environments, such as the Windows Subsystem for Linux (WSL). These environments still rely on controlled interfaces when they need services from the operating system.

  • Ntdll.dll and Win32u.dll - ntdll.dll and win32u.dll are low-level user-mode DLLs involved in transitioning requests from user mode into kernel mode. ntdll.dll exposes lower-level Native API routines. Microsoft documents some of these routines in the Windows SDK and WDK documentation, including:

    For many standard system operations, ntdll.dll contains the syscall stub that prepares the system call and transfers execution into kernel mode. win32u.dll is used for many GUI and GDI-related system calls. For example, higher-level GUI functions exposed through user32.dll and gdi32.dll may eventually reach win32u.dll before entering kernel mode.

Kernel Mode​

  • System Service Dispatcher - After a syscall enters kernel mode, the System Service Dispatcher routes the request to the appropriate kernel service routine. This dispatcher acts as the kernel-side entry point for many services requested by user-mode code.

  • Windows Executive Components - The Windows Executive contains several major kernel-mode managers responsible for core operating system functionality. These include the I/O Manager, Object Manager, Plug and Play Manager, Power Manager, Security Reference Monitor, Virtual Memory Manager, Process and Thread Manager, Configuration Manager, and Local Procedure Call components. These components are responsible for handling many important operating system tasks, including file operations, handle management, object access, memory management, process and thread creation, registry access, security checks, and communication between processes.

  • Window Manager, GDI, and Graphics Drivers - GUI and graphics-related operations follow a related but separate path. Calls from user32.dll and gdi32.dll can enter kernel mode through win32u.dll and reach kernel-mode components such as win32k.sys, win32kbase.sys, and win32kfull.sys. These components are responsible for window management and GDI services. Graphics drivers then interact with the display hardware through the Windows Display Driver Model, or WDDM.

  • Windows Kernel - The Windows kernel provides low-level operating system functionality such as scheduling, interrupt handling, exception handling, synchronization, timers, traps, and Deferred Procedure Calls, also known as DPCs. The Windows kernel image is stored on disk as ntoskrnl.exe under C:\Windows\System32\ntoskrnl.exe.

  • Hardware Abstraction Layer and Hardware - The Hardware Abstraction Layer, or HAL, provides an abstraction between Windows and the underlying hardware platform. Below the HAL is the physical hardware itself, including the CPU, memory, storage devices, network adapters, display devices, buses, interrupts, and other peripherals.

SID - (Security Identifier)​

Es un identificador de objetos en windows que Windows asigna a cada cuenta de usuario, grupo, ordenador o proceso para gestionar permisos y acceso a recursos mediante listas de control de acceso (ACL).

UN SID SE PUEDE CONVERTIR EN UN STRING LEGIBLE

SIDIdentity
S-1-1-0Everyone
S-1-5-18Local System
S-1-5-32-544Built-in Administrators group
S-1-5-32-545Built-in Users group

Ve las APIs de windows

En el contexto de Windows, el término "page" suele referirse principalmente al archivo de página (pagefile.sys), un archivo de sistema oculto que forma parte de la memoria virtual del sistema operativo. Su función principal es extender la RAM física del ordenador; cuando la memoria disponible se agota, Windows mueve datos de baja frecuencia a este archivo en el disco duro o SSD para liberar espacio en la RAM y mantener el rendimiento de las aplicaciones activas.

Virtual Memory & Paging​

Modern operating systems do not allow normal user-mode processes to directly access physical memory, meaning RAM. Instead, each process works with its own virtual address space. The operating system and processor translate virtual addresses into physical memory locations when needed.

Shared DLL image pages: Se trata de un mecanismo de gestión de memoria en Windows donde las páginas físicas de un archivo DLL compartido (como imageres.dll o shared.dll) se asignan a múltiples procesos simultáneamente para ahorrar recursos

Virtual Address Space​

Each process has its own virtual address space. Two different processes can use the same virtual address, but those addresses do not necessarily point to the same physical memory. For example, Process A and Process B may both have memory at 0x0000000000400000, but these are separate virtual addresses inside separate process address spaces.

A process can inspect its own address space using APIs such as VirtualQuery. This API receives an address and returns information about the memory region that contains it, such as the base address, region size, page state, page protection, and memory type. The returned information is stored in a MEMORY_BASIC_INFORMATION structure.

x86 vs x64 Address Space​

The amount of virtual address space available depends on the process architecture and operating system configuration.

A 32-bit process has a 4 GB virtual address range in theory, but the user-mode portion is usually smaller depending on whether the process is running on 32-bit Windows, 64-bit Windows, and whether it was linked with large-address awareness. Microsoft documents these limits in Memory Limits for Windows and Windows Server Releases

A 64-bit process on 64-bit Windows has a much larger virtual address space. Microsoft describes the 64-bit process virtual address space as being within the 128 TB range in Virtual Address Spaces.

The important point is that pointer size matters. A pointer in a 32-bit process is 4 bytes, while a pointer in a 64-bit process is 8 bytes.

Page state​

Una pagina en la memoria de windows puede tener distintos estados

  • Free: A free page is not reserved or committed. It is not accessible to the process. A free address range can later be reserved, committed, or both reserved and committed. Attempting to read from or write to a free page will usually result in an access violation.
  • A reserved page is a range of virtual address space that has been set aside for future use. The address range cannot be used by another allocation request, but it does not yet have committed storage associated with it. Reserved memory is useful when a program wants to reserve a large range of addresses and commit smaller parts of that range later. For example, VirtualAlloc can reserve memory using MEM_RESERVE.
  • A committed page has memory charges allocated from the system's available commit limit, which is backed by RAM and paging files. A committed page is accessible only if its protection allows the requested operation. Committed does not always mean the page is already resident in physical RAM. The system can bring the page into memory when the process first accesses it. This is one reason virtual memory is more flexible than directly exposing physical RAM to applications. VirtualAlloc can commit memory using MEM_COMMIT.

Page Protection Options​

  • PAGE_NOACCESS - Disables all access to the committed pages. Reading, writing, or executing this region causes an access violation.

  • PAGE_READONLY - Enables read-only access to the committed region of pages. An attempt to write to the committed region results in an access violation.

  • PAGE_READWRITE - Enables both read and write permissions to the commited region of pages.

  • PAGE_EXECUTE_READ - Enables both execution and reading on the commited region of pages.

  • PAGE_EXECUTE_READWRITE - Enables Read, Write and Execute. This is highly discouraged from being used and is generally an IoC because it's uncommon for memory to be both writable and executable at the same time.

  • PAGE_GUARD - Creates a guard page, where any attempt to access the guarded region (whether to write, execute, or ever read) will cause the system to raise an STATUS_GUARD_PAGE_VIOLATION exception and turn off the guard page status. Guard pages thus act as a one-time access alarm.

Page Type​

  1. Private Memory - A region reported as MEM_PRIVATE contains pages that are private to the process and are not mapped from a file or shared section. Memory allocated using APIs such as VirtualAlloc is generally reported as MEM_PRIVATE. Process heaps, stacks, and manually allocated buffers commonly use private memory as well.

  2. Mapped Memory - A region reported as MEM_MAPPED contains pages mapped from a section object that is not an executable image. This commonly includes views created using APIs such as CreateFileMapping and MapViewOfFile. The section may be backed by a data file or by the system paging file. Multiple processes can map the same section, allowing the underlying memory to be shared between them. The mapping APIs are covered later in details when discussing techniques like Mapping Injection.

  3. Image Memory - A region reported as MEM_IMAGE contains pages mapped from a Portable Executable image, such as an .exe or .dll (more on PE files in the next module). When Windows loads an executable image or DLL into a process, its PE sections are mapped into the process's virtual address space and are generally reported as MEM_IMAGE. The protection of each region is derived from the characteristics of its corresponding PE section. For example, the .text section is commonly mapped as executable and readable, whereas data sections may be mapped as readable and writable.

Memory Protection​

Modern Windows systems include several memory protection features that make exploitation harder and help reduce the impact of memory corruption bugs. This section will explore both Data Execution Prevention and Address Space Layout Randomization.

Data Execution Prevention - DEP​

Data Execution Prevention, commonly known as DEP, is a Windows memory protection feature that prevents code from executing from memory regions that are not explicitly marked as executable. In practice, this means that memory used for normal data, such as the stack, heap, and buffers allocated as PAGE_READWRITE, should not be treated as executable code.

DEP relies on page permissions. If a page is committed with a non-executable protection such as PAGE_READWRITE or PAGE_READONLY, the process can read or write the page depending on its protection, but it cannot execute instructions from it. If execution is attempted from that page, Windows raises an access violation. Executable pages must use one of the executable protection constants, such as PAGE_EXECUTE, PAGE_EXECUTE_READ, or PAGE_EXECUTE_READWRITE.

The reason writable and executable memory should be avoided is that it weakens this separation. If a memory region is both writable and executable, a bug or vulnerability that allows data to be written into that region may also allow that data to be executed as code. For this reason, normal programs should usually keep writable data pages and executable code pages separate. A safer pattern for programs that generate code at runtime is to write the data while the memory is writable, then change the protection with VirtualProtect so the region becomes executable but no longer writable.

Basicamente lo que previene que ejecutes código en memoria jaja lol

Address Space Layout Randomization - ASLR​

Address Space Layout Randomization, commonly known as ASLR, is another memory protection feature that makes process memory layouts less predictable. Instead of loading executable images, DLLs, stacks, heaps, and other memory regions at the same addresses every time, Windows can randomize where they appear inside a process's virtual address space.

This matters because many exploitation techniques depend on knowing where useful code or data exists in memory. For example, if a DLL always loaded at the same base address, an attacker could hardcode addresses from that DLL and reuse them across runs. ASLR makes this harder because the base address can change between process launches, system boots, or image loads, depending on the type of region and system configuration.

For Portable Executable files, ASLR depends on how the image was built. In Visual Studio, the linker option /DYNAMICBASE marks the image as compatible with ASLR and is enabled by default.

On 64-bit images, the /HIGHENTROPYVA linker option, allows Windows to use a larger address range for randomization, increasing the amount of entropy available to ASLR. This option is also enabled by default for 64-bit images, but it only has an effect when /DYNAMICBASE is enabled.

ASLR should not be confused with memory protection permissions. ASLR does not decide whether a page is readable, writable, or executable; that is handled by page protections such as PAGE_READWRITE and PAGE_EXECUTE_READ. ASLR only makes memory addresses harder to predict. This means ASLR does not prevent memory corruption bugs by itself, but it can make reliable exploitation harder by reducing address predictability.

Basicamente hace aleatoria la dirección de memoria para que se más dificil de obtener el valor.